﻿using System.Windows.Input;
using MDCAdminTool.Commands;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MDCAdminTool;
using System;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationServer.Caching.AdminApi;

namespace IntegrationTests
{
    
    
    /// <summary>
    ///This is a test class for AddCacheHostTest and is intended
    ///to contain all AddCacheHostTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AddCacheHostTest : BaseTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        public void AddCacheHostConstructorTest()
        {
            AddCacheHost target = new AddCacheHost();
        }

        /// <summary>
        ///A test for OnCanExecute
        ///</summary>
        [TestMethod()]
        [DeploymentItem("MDCAdminTool.exe")]
        public void OnCanExecuteTest()
        {
            AddCacheHost_Accessor target = new AddCacheHost_Accessor(); 
            object sender = null; 
            CanExecuteRoutedEventArgs e = makeCanExecuteRouteEventArgs(target, Hosts()[0]);
            target.OnCanExecute(sender, e);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        ///// <summary>
        /////A test for OnExecuted
        /////</summary>
        //[TestMethod()]
        //[DeploymentItem("MDCAdminTool.exe")]
        //public void testAddHosts()
        //{

        //    foreach (CacheCluster cluster in Clusters) {
        //        RevertVMs();
        //        Dictionary<String, int> addedHostDescriptors = new Dictionary<string, int>() 
        //        foreach (KeyValuePair<string, int> hostDescriptor in HostDescriptors)
        //        {
        //            cluster.AddHost(hostDescriptor.Key, hostDescriptor.Value);
        //            addedHostDescriptors.Add(hostDescriptor.Key, hostDescriptor.Value);
        //        }
        //        Assert.Co

        //    }
        //}


        /// <summary>
        ///A test for OnExecuted
        ///</summary>
        [TestMethod()]
        [DeploymentItem("MDCAdminTool.exe")]
        public void testAddHosts()
        {

            foreach (CacheCluster cluster in Clusters) {
                RevertVMs();
                AwaitVMs();
                Dictionary<String, int> addedHostDescriptors = new Dictionary<string, int>();
                foreach (KeyValuePair<string, int> hostDescriptor in HostDescriptors)
                {
                    cluster.AddHost(hostDescriptor.Key, hostDescriptor.Value);
                    addedHostDescriptors.Add(hostDescriptor.Key, hostDescriptor.Value);

                    foreach(KeyValuePair<string, int> addedHostDescriptor in addedHostDescriptors) {
                        cluster.Hosts.Single(x=> ((x.Name.Equals(hostDescriptor.Key, StringComparison.OrdinalIgnoreCase)) && (x.CachePort == hostDescriptor.Value)));
                    }

                    CacheHost addedHost = cluster.Hosts.Single(x => ((x.Name.Equals(hostDescriptor.Key, StringComparison.OrdinalIgnoreCase)) && (x.CachePort == hostDescriptor.Value)));

                    Assert.AreEqual(22235, addedHost.ArbitratorPort);
                    Assert.AreEqual(22234, addedHost.ClusterPort);
                    Assert.AreEqual(22236, addedHost.ReplicationPort);
                    Assert.AreEqual(22233, addedHost.CachePort);
                    Assert.AreEqual(90, addedHost.HighWm);
                    Assert.AreEqual(70, addedHost.LowhWm, 1);
                    Assert.AreEqual(addedHostDescriptors.Count == 1, addedHost.IsLeadHost);
                    Assert.AreEqual(Clusters[0], addedHost.Cluster);
                    Assert.AreEqual(CacheType.CacheHost, addedHost.Type);
                    Assert.AreEqual(null, addedHost.HostCacheStats);
                    Assert.AreEqual(1, addedHost.CacheNames.Count);
                    Assert.AreEqual(true, addedHost.CacheNames.All(x => x.Name.Equals("default", StringComparison.OrdinalIgnoreCase)));
                    //Assert.AreEqual(ServiceStatus.Down, addedHost.Status);

                }
                

            }
        }

        /// <summary>
        ///A test for OnExecuted
        ///</summary>
        [TestMethod()]
        [DeploymentItem("MDCAdminTool.exe")]
        public void OnExecutedTest()
        {
            //AddCacheHost_Accessor target = new AddCacheHost_Accessor(); 
            //object sender = null;
            //ExecutedRoutedEventArgs e = makeExecutedRoutedEventArgs(target, Cluster);
            //target.OnExecuted(sender, e);
            //Assert.Inconclusive("A method cannot be tested due modal window");
        }
    }
}
